/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.notify;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.NotifyExcludeResRuleCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.service.notify.NotifyExcludeResService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.NotifyExcludeResRule;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author dongyushi
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-10 上午9:14:23          dongyushi        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class NotifyExcludeResAction extends BaseAction<NotifyExcludeResRule> {

    /**
     * 
     */
    private static final long serialVersionUID = -1946313323287134459L;
    @Autowired
    private NotifyExcludeResRuleCache notifyExcludeResRuleCache;
    @Autowired
    private NotifyExcludeResService notifyExcludeResService;
    @Autowired
    private ResCache resCache;
    private Long id;
    private NotifyExcludeResRule entity;

    @Override
    public NotifyExcludeResRule getModel() {
        return entity;
    }

    public String common() {
        return "common";
    }

    @Override
    public String list() throws Exception {
        List<NotifyExcludeResRule> list = new ArrayList<NotifyExcludeResRule>();
        if (!request.getParameter("notifyRuleId").equals("")) {
            list = notifyExcludeResRuleCache.getByNotifyRule(Long.valueOf(request.getParameter("notifyRuleId")));
        }
        String resName = "";
        if (!request.getParameter("resId").equals("")) {
            resName = resCache.get(Long.valueOf(request.getParameter("resId"))).getName();
        }
        JSONObject json = new JSONObject();
        json.element("iTotalRecords", list.size());
        json.element("iTotalDisplayRecords", list.size());
        JSONArray array = new JSONArray();
        for (NotifyExcludeResRule notifyExcludeResRule : list) {
            JSONObject obj = new JSONObject();
            obj.element("op", "<div style='width: 50px'>&nbsp;&nbsp;&nbsp;<a href='javascript:del(" + notifyExcludeResRule.getId() + ");'><img src='../../style/default/bsm/images/delete.gif' title='删除'></a><div>");
            obj.element("id", notifyExcludeResRule.getId());
            obj.element("data", resName + "-->" + resCache.get(notifyExcludeResRule.getResId()).getName());
            array.add(obj);
        }
        json.element("aaData", array);
        renderText(json.toString());
        return null;
    }

    /**
     * 转向新增通知接收者页面
     * @return
     * @throws Exception
     */
    public String add() {
        Struts2Utils.getRequest().setAttribute("notifyRuleId", request.getParameter("notifyRuleId"));
        Struts2Utils.getRequest().setAttribute("resId", request.getParameter("resId"));
        return "add";
    }

    @Override
    public String input() throws Exception {
        return "update";
    }

    @Override
    public String save() throws Exception {
        if (request.getParameter("notifyRuleId").equals("")) {
            renderText("false");
            return null;
        }
        String resId = request.getParameter("resId");
        String[] array = resId.split(",");
        String notifyRuleId = request.getParameter("notifyRuleId");
        List<NotifyExcludeResRule> list = new ArrayList<NotifyExcludeResRule>();
        List<NotifyExcludeResRule> listOld = notifyExcludeResRuleCache.getByNotifyRule(Long.valueOf(notifyRuleId));
        List<Long> resIdList = new ArrayList<Long>();
        if (listOld != null) {
            for (NotifyExcludeResRule notifyExcludeResRule : listOld) {
                resIdList.add(notifyExcludeResRule.getResId());
            }
        }
        for (String s : array) {
            if (!resIdList.contains(Long.valueOf(s))) {
                NotifyExcludeResRule notifyExcludeResRule = new NotifyExcludeResRule();
                notifyExcludeResRule.setNotifyRuleId(Long.valueOf(notifyRuleId));
                notifyExcludeResRule.setResId(Long.valueOf(s));
                list.add(notifyExcludeResRule);
            }
        }
        notifyExcludeResService.batchAdd(list);
        for (NotifyExcludeResRule notifyExcludeResRule : list) {
            notifyExcludeResRuleCache.put(notifyExcludeResRule);
        }
        return null;
    }

    @Override
    public String delete() throws Exception {
        notifyExcludeResService.delete(id);
        notifyExcludeResRuleCache.remove(id);
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = notifyExcludeResRuleCache.get(id);
            if (entity == null) {
                entity = new NotifyExcludeResRule();
            }
        } else {
            entity = new NotifyExcludeResRule();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}
